﻿namespace IconPatcher
{
    using System;

    using IconPatcher.Annotations;

    using ManyConsole;

    using Microsoft.Win32;

    [UsedImplicitly]
    public class ToggleUppercaseMenus : ConsoleCommand
    {
        private const string GeneralKey = @"Software\Microsoft\VisualStudio\11.0\General";
        private const string UppercaseValueName = "SuppressUppercaseConversion";

        private bool? TurnOn;
        private bool ShowCurrent;

        public ToggleUppercaseMenus()
        {
            SkipsCommandSummaryBeforeRunning();

            IsCommand("menus", "Toggles if uppercase menus is enabled.");

            HasOption("y|on", "Turn uppercase menus on", a => TurnOn = true);
            HasOption("n|off", "Turn uppercase menus off", a => TurnOn = false);
            HasOption("t|toggle", "Toggle uppercase menus based on the current setting", a => TurnOn = null);
            HasOption("c|current", "Toggle uppercase menus based on the current setting", a => ShowCurrent = true);
        }

        public override int Run(string[] remainingArguments)
        {
            if (ShowCurrent)
            {
                var color = Console.ForegroundColor;

                var currentSetting = IsUppercaseMenus();
                if (currentSetting)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("Menus are currently uppercase");
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.WriteLine("Menus are currently lowercase");
                }

                Console.ForegroundColor = color;
            }
            else
            {
                var action = GetAction();

                SetCasing(action ? 0U : 1U);

                if (action)
                {
                    Console.WriteLine("Menus are now uppercase");
                }
                else
                {
                    Console.WriteLine("Menus are now lowercase");
                }
            }

            return 0;
        }

        private bool GetAction()
        {
            var action = TurnOn.GetValueOrDefault(true);

            if (TurnOn == null && IsUppercaseMenus())
            {
                action = false;
            }

            return action;
        }

        private static bool IsUppercaseMenus()
        {
            var general = Registry.CurrentUser.OpenSubKey(GeneralKey);
            if (general == null)
            {
                throw new Exception("Registry key (HKCU\\" + GeneralKey + ") was not found.");
            }

            return (int)general.GetValue(UppercaseValueName, 0) == 0;
        }

        private static void SetCasing(uint casing)
        {
            var general = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\VisualStudio\11.0\General", true);
            if (general == null)
            {
                throw new Exception("Registry key (HKCU\\" + GeneralKey + ") was not found.");
            }

            general.SetValue(UppercaseValueName, casing, RegistryValueKind.DWord);
        }
    }
}